<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>

<script type="text/javascript" >
    $(function(){
        // 调用专门的函数初始化分页导航条
        initPagination();

        $("#projectBody").on("click",".audit", function (){

            var projectName = $(this).parent().prev().prev().prev().prev().prev().prev().text();
            window.projectId = $(this).parent().prev().text();
                // var projectName = $(this).parent().prev().text();
            showConfirmModal(projectName);
        });

        $("#auditBtn").click(function (){

            var projectId = JSON.stringify(window.projectId);
            $.ajax({
                "url":"project/audit.json",
                "type":"post",
                "data":{ "id": window.projectId },
                "dataType":"json",
                "success":function(response){
                    var result = response.result;
                    if(result == "SUCCESS") {
                        layer.msg("操作成功！");
                        window.setTimeout(function () {
                            window.location.reload();
                        },1500)
                    }
                    if(result == "FAILED") {
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function(response){
                    layer.msg(response.status+" "+response.statusText);
                }
            });

            // 关闭模态框
            $("#auditModal").modal("hide");
        })
    });

    // 声明一个函数用于初始化 Pagination
    function initPagination() {
        // 获取分页数据中的总记录数
        var totalRecord = ${requestScope.pageInfoByProject.total};

        // 声明 Pagination 设置属性的 JSON 对象
        var properties = {
            num_edge_entries: 3,                               // 边缘页数
            num_display_entries: 5,                            // 主体页数
            callback: pageSelectCallback,                      // 用户点击“翻页”按钮之后执行翻页操作的回调函数
            current_page: ${requestScope.pageInfoByProject.pageNum-1},  // 当前页
            prev_text: "上一页",
            next_text: "下一页",
            items_per_page:${requestScope.pageInfoByProject.pageSize}   //每页要显示的数据的数量
        };
        // 调用分页导航条对应的 jQuery 对象的 pagination()方法生成导航条
        $("#Pagination").pagination(totalRecord, properties);
    }

    // 翻页过程中执行的回调函数
    function pageSelectCallback(pageIndex, jQuery){
        // pageIndex是当前页页码的索引
        var pageNum = pageIndex + 1;

        // 执行页面跳转实现翻页功能
        window.location.href = "projectAdmin/to/page.html?pageNum="+pageNum+"&keyword=${param.keyword}";

        // 取消当前超链接的默认行为
        return false;

    }

    // 声明专门的函数显示确认模态框
    function showConfirmModal(projectName) {
        // 打开模态框
        $("#auditModal").modal("show");
        // 清除旧的数据
        $("#projectNameDiv").empty();

        $("#projectNameDiv").append(projectName+"<br/>");
    }
</script>
<body>

<%@include file="/WEB-INF/include-nav.jsp" %>

<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr >
                                <th width="30">#</th>
                                <th>项目名称</th>
                                <th>发起人</th>
                                <th>目标金额（元）</th>
                                <th>众筹周期(天)</th>
                                <th>创建时间</th>
                                <th width="100">操作</th>
                                <th hidden="hidden" style="width: 5px">id</th>
                            </tr>
                            </thead>
                            <tbody id="projectBody">
                            <c:if test="${empty requestScope.pageInfoByProject.list}">
                                <tr>
                                    <td colspan="6" align="center">抱歉，没有找到您要的数据！</td>
                                </tr>
                            </c:if>
                            <c:if test="${!empty requestScope.pageInfoByProject.list}">
                                <c:forEach items="${requestScope.pageInfoByProject.list}" var="project" varStatus="myStatus">
                                <tr>
                                    <td>${myStatus.count}</td>
                                    <td>${project.projectName}</td>
                                    <td>${project.name}</td>
                                    <td>${project.money}</td>
                                    <td>${project.day}</td>
                                    <td>${project.createdate}</td>
                                    <td>${project.id}</td>
                                    <td>
                                        <button id="detail" class="btn btn-success btn-xs"><i class="glyphicon glyphicon-eye-open"></i></button>
                                        <button class="btn btn-danger btn-xs audit"><i class="glyphicon glyphicon-exclamation-sign"></i></button>
                                    </td>
                                </tr>
                                </c:forEach>
                            </c:if>
                            </tbody>
                            <tfoot>
                            <tr >
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="audit-project-confirm.jsp"%>
</body>
</html>